//
//  ViewController.m
//  TestCar
//
//  Created by 吴志勇 on 2020/2/19.
//  Copyright © 2020 wzy. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>

/** UITableView */
@property (nonatomic, weak) UITableView *tableView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    UILabel *textLabel = [[UILabel alloc] init];
    textLabel.text = @"aa";
    /**** 设置字体大小，计算宽度 ****/
    /**** 一行文字时计算宽度和高度 ****/

    textLabel.font = [UIFont systemFontOfSize:14];
    NSDictionary *textAtt = @{NSFontAttributeName:[UIFont systemFontOfSize:14]};
   CGSize size = [textLabel.text sizeWithAttributes:textAtt];
    
    //UIlabel的文字最大宽度
    textLabel.preferredMaxLayoutWidth = [UIScreen mainScreen].bounds.size.width;
    /**** 宽度确定计算高度 ****/
//    1.设置宽度和最大高度
    CGSize maxSize = CGSizeMake([UIScreen mainScreen].bounds.size.width, MAXFLOAT);
    CGRect size2 = [textLabel.text boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:textAtt context:nil];
    
    
    UITableView *tabView = [[UITableView alloc] initWithFrame:CGRectMake(40, 40, 40, 40) style:UITableViewStylePlain];
    [self.view addSubview:tabView];
    self.tableView = tabView;
    
    //进入编辑模式，左边显示删除
    [self.tableView setEditing:YES animated:YES];
    //编辑模式下可以多选
    self.tableView.allowsMultipleSelection = YES;
 
    
    tabView.delegate = self;
    tabView.dataSource =self;
    
 
    
    
   
}


#pragma mark---UITableViewDelegate
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 2;
}


/**
 *只要实现这个方法。就拥有左滑删除
 */
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    // 删除模型
    
    //刷新列表
    [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop];
}

/**
 *修改默认Delete按钮的文字
 */
-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
    return @"删除";
}


- (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    self.tableView.editing = NO; //默认是no
    
    UITableViewRowAction *action = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"关注" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
        self.tableView.editing = YES;
    }];
    
    UITableViewRowAction *action2 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"删除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
        
    }];
    
    return  @[action,action2];
}




@end

